﻿using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.SessionState;
using EazyCMS.Web.UI;
using EazyCMS.Common;
using EazyCMS.DBUtility;

namespace EazyCMS.Web.Plugin.Naire
{
    /// <summary>
    /// AJAX处理页
    /// </summary>
    public class ajax : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //取得处事类型
            string action = EazyRequest.GetQueryString("action");

            switch (action)
            {
                case "add": //用户投票
                    add(context);
                    break;
            }
        }

        #region 用户投票================================
        private void add(HttpContext context)
        {
            StringBuilder strTxt = new StringBuilder();
            BLL.vote bll = new BLL.vote();
            Model.vote model = new Model.vote();

            int vote_id = EazyRequest.GetQueryInt("vote_id");
            string code = EazyRequest.GetFormString("code");
            string remark = EazyRequest.GetFormString("remark");
            string vote_option = EazyRequest.GetFormString("option_id");
            string biaozhi = string.Empty;
            string sql = "select id from dbo.dt_naire_item where is_multi=0";
            DataTable dt = DbHelperSQL.ExecuteDataSet(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                biaozhi= "option_id" + dt.Rows[i]["id"].ToString();
                if (!string.IsNullOrEmpty(vote_option))
                {
                    vote_option += "," + EazyRequest.GetFormString(biaozhi);
                }
                else
                {
                    vote_option +=  EazyRequest.GetFormString(biaozhi);
                }

            }
            //校检验证码
            if (string.IsNullOrEmpty(code))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"对不起，请输入验证码！\"}");
                return;
            }
            if (context.Session[EazyKeys.SESSION_CODE] == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"对不起，验证码已过期！\"}");
                return;
            }
            if (code.ToLower() != (context.Session[EazyKeys.SESSION_CODE].ToString()).ToLower())
            {
                context.Response.Write("{\"status\":0, \"msg\":\"验证码与系统的不一致！\"}");
                return;
            }
            //检查投票主题
            if (vote_id == 0)
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请选择调查主题！\"}");
                return;
            }
            //检查用户投票选项
            if (string.IsNullOrEmpty(vote_option))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请选择要投票的选项！\"}");
                return;
            }
            //检查投票主题是否存在
            if (!bll.Exists(vote_id))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，投票主题不存在或已被删除！\"}");
                return;
            }
            //检查投票主题的有效期限
            //if (bll.IsExpired(vote_id))
            //{
            //    context.Response.Write("{\"status\": 0, \"msg\": \"对不起，该投票尚未开始或已结束！\"}");
            //    return;
            //}
            //检查用户是否(1天)重复投票
            //if (new BLL.vote_log().Exists(EazyRequest.GetIP(), 1))
            //{
            //    context.Response.Write("{\"status\": 0, \"msg\": \"投票失败，您已经投过票了！\"}");
            //    return;
            //}
            //保存用户投票
            if (bll.AddVote(vote_id, Utils.ToHtml(remark), vote_option))
            {
                context.Response.Write("{\"status\": 1, \"msg\": \"投票已成功，感谢您的参与！\"}");
                return;
            }
            context.Response.Write("{\"status\": 0, \"msg\": \"投票失败，请联系管理员！\"}");
            return;
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}